## Authors: Alexander Herzog and Kenneth Benoit
## Date: May 30, 2015
## Replication file for JOP article "The Most Unkindest Cuts: Speaker Selection and Expressed Government Dissent During Economic Crisis"

rm(list = ls(all = TRUE))

library(ggplot2)

### DATA ##############################
load("./generated_data/working_data_speakers.RData")
########################################
data <- dataSub

# Variables
# ---------
# election years
election.year <- c(1987.2,1989.5,1992.9,1997.5,2002.3,2007.3,2011.1)


# Plot of average positions over time
# -----------------------------------
# group
data$group <- NA
data$group[data$position=="Opposition"] <- 1
data$group[data$position=="Govt backbencher"] <- 2
data$group[data$position %in% c("Taoiseach","Tánaiste","Minister","Minister of State")] <- 3

data$group <- factor(data$group, labels=c("Opposition","Backbench","Cabinet"))

# construct data set
avgPos <- aggregate(data$textscore, by=list(data$debate_dateStart,data$group), mean)
names(avgPos) <- c("debate_dateStart","group","mean")

avgMin <- aggregate(data$textscore, by=list(data$debate_dateStart,data$group), min)
names(avgMin) <- c("debate_dateStart","group","min")

avgMax <- aggregate(data$textscore, by=list(data$debate_dateStart,data$group), max)
names(avgMax) <- c("debate_dateStart","group","max")

avgSd <- aggregate(data$textscore, by=list(data$debate_dateStart,data$group), sd)
names(avgSd) <- c("debate_dateStart","group","sd")

sumStats <- merge(avgPos,avgMin)
sumStats <- merge(sumStats,avgMax)
sumStats <- merge(sumStats,avgSd)

limits <- aes(ymin=mean-sd, ymax=mean+sd)
#limits <- aes(ymin=min, ymax=mean)



# plot mean positions
p <- ggplot(data=sumStats, aes(x=debate_dateStart, y=mean, group=group, color=group)) +
    theme(panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank(),
          axis.line = element_line(colour = "black")) +
    geom_point() +
    geom_line(linetype=1, alpha=0.5) +
    geom_smooth(alpha=0.2, linetype=2) +
    xlab("") +
    ylab("Average position") +
    guides(color = guide_legend(title = "Group:")) +
    scale_x_date(breaks = sort(unique(sumStats$debate_dateStart)), labels=sort(unique(data$budget_year))) +
    theme(axis.text.x = element_text(angle = 30)) +   
    scale_y_continuous(breaks=seq(-0.5,0.5,0.1)) +
    theme(legend.position = "bottom") +
    geom_vline(xintercept = as.numeric(sort(unique(as.Date(data$govtStart)))), dcolor="black", linetype="solid", col="grey") +     
    annotate("text", x=as.Date("1988-05-15"), y=0.5, label="FF Minority\n", size=4) +
    annotate("text", x=as.Date("1992-04-01"), y=0.5, label="FF Coalitions\n", size=4) +
    annotate("text", x=as.Date("1996-04-01"), y=0.5, label="FG-Lab-DL\nCoalition", size=4) +
    annotate("text", x=as.Date("2002-09-01"), y=0.5, label="FF-led Boom Years\n", size=4) +
    annotate("text", x=as.Date("2009-10-01"), y=0.5, label="FF-Greens\nCrisis Years", size=4) +
    annotate("text", x=as.Date("2012-08-01"), y=0.5, label="FG-Lab\nCrisis Years", size=4)

pdf(file="./plots/avg_positions_over_time.pdf", height=6, width=12)
print(p)
dev.off()


# Time series plot of interquartile range for govt vs opposition
# --------------------------------------------------------------------------
sumStats <- aggregate(data$textscore, by=list(data$debate_dateStart,data$govt), IQR)
names(sumStats) <- c("debate_dateStart","group","iqr")

# plot std dev
p <- ggplot(data=sumStats, aes(x=debate_dateStart, y=iqr, group=group, color=group)) +
    theme(panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank(),
          axis.line = element_line(colour = "black")) +
    geom_point() +
    geom_line(linetype=1, alpha=0.5) +
    geom_smooth(alpha=0.2, linetype=2) +
    xlab("") +
    ylab("Interquartile range") +
    guides(color = guide_legend(title = "Group:")) +
    scale_x_date(breaks = sort(unique(sumStats$debate_dateStart)), labels=sort(unique(data$budget_year))) +
    theme(axis.text.x = element_text(angle = 30)) +   
    scale_y_continuous(breaks=seq(0,1,0.1)) +
    theme(legend.position = "bottom") +
    geom_vline(xintercept = as.numeric(sort(unique(as.Date(data$govtStart)))), dcolor="black", linetype="solid", col="grey") +     
    annotate("text", x=as.Date("1988-05-15"), y=0.7, label="FF Minority\n", size=4) +
    annotate("text", x=as.Date("1992-04-01"), y=0.7, label="FF Coalitions\n", size=4) +
    annotate("text", x=as.Date("1996-04-01"), y=0.7, label="FG-Lab-DL\nCoalition", size=4) +
    annotate("text", x=as.Date("2002-09-01"), y=0.7, label="FF-led Boom Years\n", size=4) +
    annotate("text", x=as.Date("2009-10-01"), y=0.7, label="FF-Greens\nCrisis Years", size=4) +
    annotate("text", x=as.Date("2012-08-01"), y=0.7, label="FG-Lab\nCrisis Years", size=4)


pdf(file="./plots/interquartile_range_over_time.pdf", height=6, width=12)
print(p)
dev.off()


